home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH3 / SRC / RGBCOLOR.FRM < prev    next >
Text File  |  1997-01-03  |  8KB  |  288 lines

  1. VERSION 4.00
  2. Begin VB.Form RGBColorForm 
  3.    Caption         =   "RGBColor"
  4.    ClientHeight    =   3030
  5.    ClientLeft      =   2055
  6.    ClientTop       =   1605
  7.    ClientWidth     =   5010
  8.    Height          =   3720
  9.    Left            =   1995
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   202
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   334
  14.    Top             =   975
  15.    Width           =   5130
  16.    Begin VB.PictureBox DefaultPict 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   1650
  19.       Left            =   0
  20.       ScaleHeight     =   106
  21.       ScaleMode       =   3  'Pixel
  22.       ScaleWidth      =   106
  23.       TabIndex        =   13
  24.       Top             =   0
  25.       Width           =   1650
  26.    End
  27.    Begin VB.HScrollBar BlueScroll 
  28.       Height          =   255
  29.       LargeChange     =   16
  30.       Left            =   900
  31.       Max             =   255
  32.       TabIndex        =   7
  33.       Top             =   2760
  34.       Width           =   4080
  35.    End
  36.    Begin VB.HScrollBar GreenScroll 
  37.       Height          =   255
  38.       LargeChange     =   16
  39.       Left            =   900
  40.       Max             =   255
  41.       TabIndex        =   6
  42.       Top             =   2400
  43.       Width           =   4080
  44.    End
  45.    Begin VB.PictureBox CustomPict 
  46.       AutoRedraw      =   -1  'True
  47.       Height          =   1650
  48.       Left            =   3360
  49.       Picture         =   "RGBCOLOR.frx":0000
  50.       ScaleHeight     =   106
  51.       ScaleMode       =   3  'Pixel
  52.       ScaleWidth      =   106
  53.       TabIndex        =   5
  54.       Top             =   0
  55.       Width           =   1650
  56.    End
  57.    Begin VB.HScrollBar RedScroll 
  58.       Height          =   255
  59.       LargeChange     =   16
  60.       Left            =   900
  61.       Max             =   255
  62.       TabIndex        =   4
  63.       Top             =   2040
  64.       Width           =   4080
  65.    End
  66.    Begin VB.PictureBox RainbowPict 
  67.       AutoRedraw      =   -1  'True
  68.       Height          =   1650
  69.       Left            =   1680
  70.       Picture         =   "RGBCOLOR.frx":0446
  71.       ScaleHeight     =   106
  72.       ScaleMode       =   3  'Pixel
  73.       ScaleWidth      =   106
  74.       TabIndex        =   0
  75.       Top             =   0
  76.       Width           =   1650
  77.    End
  78.    Begin VB.Label Label1 
  79.       Alignment       =   2  'Center
  80.       Caption         =   "Rainbow Palette"
  81.       Height          =   255
  82.       Index           =   5
  83.       Left            =   1680
  84.       TabIndex        =   14
  85.       Top             =   1680
  86.       Width           =   1650
  87.    End
  88.    Begin VB.Label BlueLabel 
  89.       BorderStyle     =   1  'Fixed Single
  90.       Height          =   255
  91.       Left            =   480
  92.       TabIndex        =   12
  93.       Top             =   2760
  94.       Width           =   375
  95.    End
  96.    Begin VB.Label GreenLabel 
  97.       BorderStyle     =   1  'Fixed Single
  98.       Height          =   255
  99.       Left            =   480
  100.       TabIndex        =   11
  101.       Top             =   2400
  102.       Width           =   375
  103.    End
  104.    Begin VB.Label RedLabel 
  105.       BorderStyle     =   1  'Fixed Single
  106.       Height          =   255
  107.       Left            =   480
  108.       TabIndex        =   10
  109.       Top             =   2040
  110.       Width           =   375
  111.    End
  112.    Begin VB.Label Label1 
  113.       Alignment       =   2  'Center
  114.       Caption         =   "Customized Palette"
  115.       Height          =   255
  116.       Index           =   4
  117.       Left            =   3360
  118.       TabIndex        =   9
  119.       Top             =   1680
  120.       Width           =   1650
  121.    End
  122.    Begin VB.Label Label1 
  123.       Alignment       =   2  'Center
  124.       Caption         =   "Default Palette"
  125.       Height          =   255
  126.       Index           =   3
  127.       Left            =   0
  128.       TabIndex        =   8
  129.       Top             =   1680
  130.       Width           =   1650
  131.    End
  132.    Begin VB.Label Label1 
  133.       Caption         =   "Blue"
  134.       Height          =   255
  135.       Index           =   2
  136.       Left            =   0
  137.       TabIndex        =   3
  138.       Top             =   2760
  139.       Width           =   495
  140.    End
  141.    Begin VB.Label Label1 
  142.       Caption         =   "Green"
  143.       Height          =   255
  144.       Index           =   1
  145.       Left            =   0
  146.       TabIndex        =   2
  147.       Top             =   2400
  148.       Width           =   495
  149.    End
  150.    Begin VB.Label Label1 
  151.       Caption         =   "Red"
  152.       Height          =   255
  153.       Index           =   0
  154.       Left            =   0
  155.       TabIndex        =   1
  156.       Top             =   2040
  157.       Width           =   495
  158.    End
  159.    Begin VB.Menu mnuFile 
  160.       Caption         =   "&File"
  161.       Begin VB.Menu mnuFileExit 
  162.          Caption         =   "E&xit"
  163.       End
  164.    End
  165. End
  166. Attribute VB_Name = "RGBColorForm"
  167. Attribute VB_Creatable = False
  168. Attribute VB_Exposed = False
  169. Option Explicit
  170.  
  171. Dim CustomPalette As Integer
  172. Dim wid As Single
  173. Dim hgt As Single
  174.  
  175. ' ***********************************************
  176. ' Resize CustomPict's palette so it has only one
  177. ' entry. We will use that entry to display the
  178. ' color selected by the scroll bars.
  179. ' ***********************************************
  180. Sub ShrinkPalette()
  181.     CustomPalette = CustomPict.Picture.hPal
  182.     If ResizePalette(CustomPalette, 1) = 0 Then
  183.         Beep
  184.         MsgBox "Error resizing palette.", vbExclamation
  185.     End If
  186. End Sub
  187.  
  188.  
  189. ' ***********************************************
  190. ' Display the selected RGB value in both picture
  191. ' boxes.
  192. ' ***********************************************
  193. Sub UpdateColors()
  194. Dim r As Integer
  195. Dim g As Integer
  196. Dim b As Integer
  197. Dim palentry As PALETTEENTRY
  198. Dim status As Integer
  199.  
  200.     r = RedScroll.Value
  201.     g = GreenScroll.Value
  202.     b = BlueScroll.Value
  203.     
  204.     ' Update the numeric labels.
  205.     RedLabel.Caption = Format$(r)
  206.     GreenLabel.Caption = Format$(g)
  207.     BlueLabel.Caption = Format$(b)
  208.     
  209.     ' Display the color in the default picture.
  210.     DefaultPict.Line (0, 0)-Step(wid, hgt), RGB(r, g, b), BF
  211.  
  212.     ' Display the color in the rainbow picture.
  213.     RainbowPict.Line (0, 0)-Step(wid, hgt), RGB(r, g, b), BF
  214.  
  215.     ' Put the new color in the custom palette.
  216.     palentry.peRed = r
  217.     palentry.peGreen = g
  218.     palentry.peBlue = b
  219.     If SetPaletteEntries(CustomPalette, 0, 1, palentry) = 0 Then
  220.         Beep
  221.         MsgBox "Error updating palette entry.", vbExclamation
  222.     End If
  223.     
  224.     ' Make the change take effect.
  225.     status = RealizePalette(CustomPict.hdc)
  226.     
  227.     ' Fill the custom palette picture.
  228.     CustomPict.Line (0, 0)-Step(wid, hgt), RGB(r, g, b) + &H2000000, BF
  229. End Sub
  230.  
  231. Private Sub BlueScroll_Change()
  232.     UpdateColors
  233. End Sub
  234.  
  235. Private Sub BlueScroll_Scroll()
  236.     UpdateColors
  237. End Sub
  238.  
  239.  
  240. Private Sub Form_Load()
  241.     ' Make sure the screen supports palettes.
  242.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  243.         Beep
  244.         MsgBox "This monitor does not support palettes.", _
  245.             vbCritical
  246.         End
  247.     End If
  248.  
  249.     ' Save the lower right corner of the picture
  250.     ' boxes for easier drawing later.
  251.     wid = DefaultPict.ScaleWidth - 1
  252.     hgt = DefaultPict.ScaleHeight - 1
  253.     
  254.     ' Load the system palette.
  255.     ShrinkPalette
  256.  
  257.     ' Display the initial color (black).
  258.     UpdateColors
  259. End Sub
  260.  
  261.  
  262. Private Sub GreenScroll_Change()
  263.     UpdateColors
  264. End Sub
  265.  
  266. Private Sub GreenScroll_Scroll()
  267.     UpdateColors
  268. End Sub
  269.  
  270.  
  271.  
  272. Private Sub mnuFileExit_Click()
  273.     Unload Me
  274. End Sub
  275.  
  276.  
  277.  
  278. Private Sub RedScroll_Change()
  279.     UpdateColors
  280. End Sub
  281.  
  282.  
  283. Private Sub RedScroll_Scroll()
  284.     UpdateColors
  285. End Sub
  286.  
  287.  
  288.